<template>
  <el-button v-if="!this.snapshotMode" text
             type="primary"
             size="small"
             icon="el-icon-plus"
             @click="$refs.crud.rowAdd()">
    增加参数
  </el-button>
  <avue-crud ref="crud"
             v-show="modelValue.length > 0"
             :data="modelValue"
             v-model="form"
             :option="option"
             @row-save="rowSave"
             @row-update="rowUpdate"
             @row-del="rowDel"></avue-crud>
</template>
<script>

export default {
  name: "functionEnum",
  props: {
    modelValue: {
      type: Object,
      default: () => ([])
    },
    snapshotMode: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {},
      data: [],
      option: {
        size: 'small',
        header: false,
        labelWidth: 100,
        menuWidth: 130,
        dialogWidth: 600,
        column: [{
          label: '参数值',
          labelTip: '取值范围：-2147483648 ~ 2147483647',
          prop: "enumKey",
          placeholder: '枚举编号比如:0',
          type: "number",
          min: -2147483648,
          max: 2147483647,
          span: 24,
          rules: [{
            required: true,
            message: "整型参数，取值范围：-2147483648 ~ 2147483647",
            trigger: "change"
          }],
        }, {
          label: '参数描述',
          labelTip: '支持中文、英文大小写、日文、数字、下划线和短划线，必须以中文、英文或数字开头，不超过20个字符',
          prop: "enumValue",
          placeholder: '对该枚举的描述',
          span: 24,
          rules: [{
            required: true,
            message: "请输入参数描述",
            trigger: "change"
          }],
        }]
      },
    };
  },
  mounted() {
    // 在这里可以安全地进行DOM操作
    this.$nextTick(() => {
      // 代码在这里确保整个视图已经更新完毕
      if (this.snapshotMode) {
        this.$refs.crud.option.editBtn = false;
        this.$refs.crud.option.delBtn = false;
        this.$refs.crud.option.viewBtn = true;
      } else {
        this.$refs.crud.option.editBtn = true;
        this.$refs.crud.option.delBtn = true;
        this.$refs.crud.option.viewBtn = false;
      }
    });
  },
  methods: {
    rowSave(form, done, loading) {
      form.id = new Date().getTime();
      loading();
      done(form);
    },
    rowUpdate(form, index, done, loading) {
      loading();
      done(form);
    },
    rowDel(form, index, done) {
      this.$confirm('确定将选择数据删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        done(form)
      });
    },
  }
}
</script>
